package com.ryskoo.controller.cms;

import java.util.List;

import com.ryskoo.cms.advert.domain.AdvertPO;
import com.ryskoo.cms.advert.service.IAdvertService;
import com.ryskoo.cms.advertpicrel.domain.AdvertPicRelPO;
import com.ryskoo.cms.advertpicrel.service.IAdvertPicRelService;
import com.ryskoo.framework.core.bean.QueryConditionEnum;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.exception.InvalidParamException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 广告管理web controller
 *
 * @author thq
 */
@Controller
@RequestMapping(value = "/vendor/cms/advert")
public class AdvertController extends BasicHandler<AdvertPO> {

    private IAdvertService advertService;

    public AdvertController() {
        super("system/cms/advert", "Advert");
    }
    
    @Autowired
    private IAdvertPicRelService advertPicRelService;

    @Autowired
    public void setService(IAdvertService advertService) {
        this.advertService = advertService;
        super.setService(advertService);
    }
    
    
	/**
	 * 根据广告id获取已经关联的广告图片
	 * @param advertId
	 * @return
	 */
	@PostMapping("/getpictures")
	@ResponseBody
	public ReturnMessageDTO getPictures(@RequestParam(value = "advertId") String advertId) {
		 ReturnMessageDTO<List<AdvertPicRelPO>> returnMessage = new ReturnMessageDTO<List<AdvertPicRelPO>>();
        try {
            //根据广告id获取图片
            QueryMessageDTO query = new QueryMessageDTO();
            query.addCondition("advertId", QueryConditionEnum.EQ.name() , advertId);
            query.addSort("position", "asc");
            List<AdvertPicRelPO> pictures = advertPicRelService.queryByCondition(query);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnMessage(pictures);
            returnMessage.setReturnNote("获取广告图片成功");
		} catch (Exception e) {
			returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("获取广告图片失败");
		}
        return returnMessage;
	}
	
	
	/**
	 * 根据广告id,保存广告关联的图片
	 * @param advertId
	 * @return
	 */
	@PostMapping("/savepictures")
	@ResponseBody
	public ReturnMessageDTO<String> savePictures(@RequestParam(value = "advertId") String advertId, @RequestParam(value = "pics") String pics) {
        ReturnMessageDTO<String> returnMessage = new ReturnMessageDTO<String>();
        try {
			advertPicRelService.saveAdvertPicture(advertId, pics);
			returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
	        returnMessage.setReturnNote("保存广告图片成功");
		} catch (InvalidParamException e) {
			returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
	        returnMessage.setReturnNote(e.getMessage());
		}catch (Exception e) {
			returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
	        returnMessage.setReturnNote("保存广告图片失败");
		}
        return returnMessage;
	}

}
